// pages/hotelDetail/hotelDetail.js
let app=getApp();
var util = require('../../utils/util.js');

var startDate = '';
var startYear;
var startDay;
var startMonth;
var startWeek;
var endOfStartDate = '2020-12-31';
var startDayCount;

var endDate = '';
var endYear;
var endDay;
var endMonth;
var endWeek;
var endOfEndDate = '2020-12-31';

var dayCount = 1;
var liveNum;

function RoomBean() {
     var image;
     var name;
     var service;
     var price;
}


Page({

     /**
      * 页面的初始数据
      */
     data: {
          startDate: '',
          currentDate: '',
          endOfStartDate: '',
          endDate: '',
          endOfEndDate: '',
          startDay: '',
          startMonth: '',
          startWeek: '',
          endDay: '',
          endMonth: '',
          endWeek: '',
          dayCount: 1,


          roomTypeName: '',
          type: '',
          typeId:0,
          liveNum,
          roomArray: [],
          serviceList: [
               {
                    icon: '../../res/images/ic_service_park.png',
                    name: '停车场'
               },
               {
                    icon: '../../res/images/ic_service_food.png',
                    name: '营养早餐'
               },
               {
                    icon: '../../res/images/ic_service_park.png',
                    name: '健身室'
               },
               {
                    icon: '../../res/images/ic_service_food.png',
                    name: '免费WiFi'
               },
               {
                    icon: '../../res/images/ic_service_park.png',
                    name: '叫车服务'
               },
               {
                    icon: '../../res/images/ic_service_food.png',
                    name: '营养早餐'
               },
               {
                    icon: '../../res/images/ic_service_park.png',
                    name: '健身室'
               },
               {
                    icon: '../../res/images/ic_service_food.png',
                    name: '免费WiFi'
               },
               {
                    icon: '../../res/images/ic_service_park.png',
                    name: '叫车服务'
               }
          ]
     },

     /**
      * 生命周期函数--监听页面加载
      */
     onLoad: function (options) {
          startDate=app.globalData.startDate;
          var start=startDate.split("-");
          startYear = start[0];
          startMonth = start[1];
          startDay = start[2];
          startWeek = new Date(startDate).getDay();
          
          endDate=app.globalData.endDate;
          var end=endDate.split("-");
          endYear = end[0];
          endMonth = end[1];
          endDay = end[2];
          endWeek = new Date(endDate).getDay();

          this.setSearchDate();

          var roomTypeName = options.name;
          var bedNum = options.bedNum;
          liveNum = options.liveNum;
          //根据传进来的id获取剩余的房间
          var typeId=options.id;
          if (roomTypeName != null) {
               this.setData({
                    roomTypeName: roomTypeName,
                    type: bedNum+'张床位,可住'+liveNum+'人',
                    typeId:typeId,
                    liveNum:liveNum
               });
          }
          var that=this;
          //获取当前房间类型剩余房间
          wx.request({
              url:"http://101.37.14.167:8001/Room/findByRoomTypeAndStatus",
              method:"POST",
              header:{'content-type':'application/x-www-form-urlencoded'},
              data:{
                   id:this.data.typeId,
                   status:0
              },
              success: function(res){  //若成功，根据返回值生成房间列表
                    var roomArray=[];
                    var list=res.data.data;
                    if(list.length!=0){
                         for(var i=0;i<list.length;i++){
                              var roomBean=new RoomBean();
                              roomBean.image=list[i].photo;
                              roomBean.name=list[i].sn;
                              roomBean.service=list[i].remark;
                              roomBean.price=list[i].roomType.price;
                              roomArray.push(roomBean);
                         }
                    }
                    that.setData({
                         roomArray:roomArray
                    })
              },
              fail:function(res){
                    console.log("失败"+res.data);
                    wx.showToast({
                         title:'请求失败',
                         icon:'none',
                         duration:2000
                    });
              }
          })
     },
     //点击预订跳转到支付页面,若未绑定信息则跳转页面
     bookRoom: function (e) {
          var index = e.currentTarget.dataset.index;
          var room = this.data.roomArray[index];
          var isSetInfo=app.globalData.isSetInfo;//根据isSetInfo判断是否绑定信息
          if(isSetInfo==true){
               wx.navigateTo({
                    url: '../bookHotel/bookHotel?price=' + room.price + '&roomTypeName=' + this.data.roomTypeName + '&roomName=' + room.name + '&startDate=' + startDate + '&endDate=' + endDate+'&dayCount='+this.data.dayCount+'&typeId='+this.data.typeId+'&liveNum='+this.data.liveNum,
               })
          }
          else{
               wx.showModal({
                    title: '提示',
                    content: '你需要登录并绑定信息',
                    showCancel:false,
                    success: function (res) {
                         if (res.confirm) {
                              wx.switchTab({
                                   url:'../minePage/minePage'
                              })
                         }
                    }
               })
          }
     },

     startDateChange: function (e) {
          startDate = e.detail.value;
          var startArray = startDate.split('-');
          startYear = parseInt(startArray[0]);
          startDay = parseInt(startArray[2]);
          startMonth = parseInt(startArray[1]);
          startWeek = new Date(startDate).getDay();

          var startFormat = this.formatDate(startDate);
          var endFormat = this.formatDate(endDate);
          if (new Date(endFormat) < new Date(startFormat)) {
               this.initEndDate();
          }

          this.setSearchDate();
     },

     endDateChange: function (e) {
          endDate = e.detail.value;
          var endArray = endDate.split('-');
          endYear = parseInt(endArray[0]);
          endDay = parseInt(endArray[2]);
          endMonth = parseInt(endArray[1]);
          endWeek = new Date(endDate).getDay();

          var startFormat = this.formatDate(startDate);
          var endFormat = this.formatDate(endDate);
          var endTime=new Date(endFormat).getTime();
          var startTime=new Date(startFormat).getTime();
          if (endTime<=startTime) {
               endDate=new Date(startDate);
               endDate = util.transTime(endDate.getTime() +  1000 * 60 * 60 * 24);
               var a = endDate.split("-");
               a[1]=this.prefixInteger(a[1],2);
               a[2]=this.prefixInteger(a[2],2);
               endYear=a[0];
               endMonth=a[1];
               endDay=a[2];
               endWeek=new Date(endDate).getDay();
          }

          this.setSearchDate();
     },

     formatDate: function (date) {
          return date.replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '').replace(/(-)/g, '/');
     },
     getWeekday: function (week) {
          var weekday = new Array(7)
          weekday[0] = "周日"
          weekday[1] = "周一"
          weekday[2] = "周二"
          weekday[3] = "周三"
          weekday[4] = "周四"
          weekday[5] = "周五"
          weekday[6] = "周六"

          return weekday[week];
     },

     prefixInteger: function (num, length) {
          return (Array(length).join('0') + num).slice(-length);
     },

     getDayCount: function (startDate, endDate) {
          var startFormat = this.formatDate(startDate);
          var endFormat = this.formatDate(endDate);

          var start = new Date(startFormat);
          var end = new Date(endFormat);

          var result = end - start;
          if (result >= 0) {
               var days = parseInt(result / (1000 * 60 * 60 * 24));
               return days == 0 ? 1 : days;
          } else {
               return 0;
          }
     },

     initEndDate: function () {
          if (startMonth == 12 && startDay == 31) {
               endYear = startYear + 1;
               endMonth = 1;
               endDay = 1;
          } 
          else {
               endDate=new Date(startDate);
               endDate = util.transTime(endDate.getTime() +  1000 * 60 * 60 * 24);
               var a = endDate.split("-");
               a[1]=this.prefixInteger(a[1],2);
               a[2]=this.prefixInteger(a[2],2);
               endYear=a[0];
               endMonth=a[1];
               endDay=a[2];
          }
          endDate = endYear + '-' + endMonth + '-' + endDay;
          endWeek = new Date(endDate).getDay();
     },

     setSearchDate: function () {
          this.setData({
               startDate: startDate,
               startDay: this.prefixInteger(startDay, 2),
               startMonth: this.prefixInteger(startMonth, 2),
               startWeek: this.getWeekday(startWeek),
               endOfStartDate: '2020-12-31',

               endDate: endDate,
               endDay: this.prefixInteger(endDay, 2),
               endMonth: this.prefixInteger(endMonth, 2),
               endWeek: this.getWeekday(endWeek),
               endOfEndDate: '2020-12-31',

               dayCount: this.getDayCount(startDate, endDate)
          });
          if(!(app.globalData.startDate==this.data.startDate&&app.globalData.endDate==this.data.endDate)){
               app.globalData.startDate=this.data.startDate;
               app.globalData.endDate=this.data.endDate;
          }
     },


     /**
      * 生命周期函数--监听页面初次渲染完成
      */
     onReady: function () {

     },

     /**
      * 生命周期函数--监听页面显示
      */
     onShow: function () {

     },

     /**
      * 生命周期函数--监听页面隐藏
      */
     onHide: function () {

     },

     /**
      * 生命周期函数--监听页面卸载
      */
     onUnload: function () {

     },

     /**
      * 页面相关事件处理函数--监听用户下拉动作
      */
     onPullDownRefresh: function () {

     },

     /**
      * 页面上拉触底事件的处理函数
      */
     onReachBottom: function () {

     },

     /**
      * 用户点击右上角分享
      */
     onShareAppMessage: function () {

     }
})